package P1线性结构;

public class TestJosephusLoop {
    public static void main(String[] args) {
        //从链表的角度来考虑约瑟夫环问题：从节点的角度去操作
        LinkedListSinglyCircle<Integer> list = new LinkedListSinglyCircle<>();
        for (int i = 1; i <= 41; i++) {
            list.add(i);
        }
        System.out.println(list);
        list.josephusLoop();
        System.out.println(list);
        //从数组的角度来考虑约瑟夫环问题：
        ArrayList<Integer> arr = new ArrayList<>();
        for (int i = 1; i <= 41; i++) {
            arr.add(i);
        }
        System.out.println(arr);
        /*
        1 2 3 4 5 6
        0 1 2 3 4 5
            p

        1 2 4 5 6
        0 1 2 3 4
                p

        1 2 4 5
        0 1 2 3 4 5 6
            p

        1 2 5
        0 1 2 3 4
          p
        */
        int p = 0;
        while (arr.size() > 2) {
            p += 2;
            p = p % arr.size();
            arr.remove(p);
        }
        System.out.println(arr);
    }
}
